package kr.co.valueworks.fishing.activity;

import kr.co.valueworks.fishing.R;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

public class AreaActivity extends BaseActivity{

	final String AD_UNIT_ID = "a14dd47eed69320";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.area_list);
		
		//AdView adView = new AdView(this, AdSize.BANNER, AD_UNIT_ID);
		//LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
		//layout.addView(adView);
		AdView adView = (AdView)findViewById(R.id.adView);
	    AdRequest request = new AdRequest();
		adView.loadAd(request);
		 
		final EditText editText = (EditText)findViewById(R.id.searchEditText);
		editText.setOnEditorActionListener(new OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if(actionId == EditorInfo.IME_ACTION_SEARCH){ 		
					goSearch();
				}
				return false;
			}
		});
		ImageButton imgBtn = (ImageButton)findViewById(R.id.searchBtn);
		imgBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				goSearch();
			}
		});
			
		/*
		final ArrayAdapter<CharSequence> adapter;
		adapter = ArrayAdapter.createFromResource(this, R.array.area,
				android.R.layout.simple_list_item_1);
		*/
		String[] areas = getResources().getStringArray(R.array.area);
		final AreaAdapter adapter = new AreaAdapter(this,areas);
		ListView list = (ListView)findViewById(R.id.areaListView);
		list.setAdapter(adapter);
		
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
					
					String area = adapter.getItem(position).toString();
					Intent intent = new Intent(AreaActivity.this, LocalityActivity.class);
		    		intent.putExtra("area", area);
					startActivity(intent);					
				
			}
		});
	}
	
	protected void goSearch()
	{
		EditText editText = (EditText)findViewById(R.id.searchEditText);
		String keyword = editText.getText().toString().trim();
		if(keyword.equals("")) return;
		Intent intent = new Intent(AreaActivity.this, Fishing.class);
		intent.putExtra("type", "search");
		intent.putExtra("keyword", keyword);
		startActivity(intent);			
	}

}

class AreaAdapter extends BaseAdapter {
	LayoutInflater mInflater;
	String[] arSrc;

	public AreaAdapter(Context context, String[] arItem) {
		mInflater = (LayoutInflater)context.getSystemService(
				Context.LAYOUT_INFLATER_SERVICE);
		arSrc = arItem;
	}

	@Override
	public int getCount() {
		return arSrc.length;
	}

	@Override
	public Object getItem(int position) {
		return arSrc[position];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = mInflater.inflate(R.layout.area_item, parent, false);
		
		TextView area = (TextView)convertView.findViewById(R.id.areaTextView);	
		area.setText(arSrc[position]);
		
		return convertView;		
	}

}
